#!/bin/bash

# Set the base directory as the current directory (replication_files_jop_dataverse)
BASE_DIR="."
cd "$BASE_DIR"

# ------------------------------
# Pre-create expected folders
# ------------------------------
echo "Creating expected folder structure..."
mkdir -p results/figures
mkdir -p results/in_text
mkdir -p results/tables
mkdir -p data/1_raw_data
mkdir -p data/2_processed_data
mkdir -p data/3_model_output

echo "Folder structure created."

# ------------------------------
# Process each file
# ------------------------------
for file in *; do
  if [[ -f "$file" ]]; then
    # Skip files without dashes (optional)
    if [[ "$file" != *-* ]]; then
      echo "Skipping $file (no dashes)"
      continue
    fi

    # Extract folder structure and filename
    folder_part="${file%.*}"  # Remove extension for parsing
    extension="${file##*.}"   # Get file extension
    
    # Extract folder path and filename
    folder_path=$(echo "$folder_part" | rev | cut -d'-' -f2- | rev | tr '-' '/')
    base_filename=$(echo "$folder_part" | rev | cut -d'-' -f1 | rev).$extension

    # Create the folder structure if needed
    mkdir -p "$folder_path"

    # Move the file into the folder
    mv "$file" "$folder_path/$base_filename"
    
    echo "Moved $file to $folder_path/$base_filename"
  fi
done

echo "All files restored to their original folder structure."